<?php

namespace App\Http\Controllers\Mini;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\TeacherModel;

class TeacherController extends Controller
{
    public function list(Request $request) {
        $teachers = TeacherModel::all();
        return self::ok($teachers);
    }
    public function info(Request $request) {
        $openid = $request->header('openid');
        $teacher = TeacherModel::where('wx_id', $openid)->first();
        if ($teacher) {
            $img = $teacher->img;
            $teacher->img = $img ? url($img) : '';
            return self::ok($teacher, '获取成功');
        }
    }

    public function edit(Request $request) {
        $openid = $request->header('openid');
        $teacher = TeacherModel::where('wx_id', $openid)->first();
        if (!$teacher) {
           return self::err('用户不存在');
        }

        $data = $request->only(['name', 'nickname', 'phone', 'sex', 'birthday']);
        $teacher->update($data);
        return self::ok($teacher, '修改成功');
    }
}
